package com.hmkj.core.mapper.order;

import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.core.model.order.OrderModel;
import com.hmkj.core.po.order.Order;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 策略表相关Dao接口<br>
 *
 * @author eproo
 * @date 2017-11-07 06:58:23
 */
public interface OrderMapper extends GenericMapper<Order, Integer> {

    @Select("SELECT\n" +
            "\tt.*,u.user_name AS 'u.userName',u.mobile AS 'u.mobile',iu.user_name AS 'iu.userName',iu.mobile AS 'iu.mobile',ag.name AS 'ag.name'\n" +
            "FROM\n" +
            "\tbiz_order t \n" +
            "LEFT JOIN biz_user u ON t.user_id = u.id \n" +
            "LEFT JOIN biz_user iu ON t.invest_id = iu.id\n" +
            "LEFT JOIN biz_agent ag ON t.agent_id = ag.id\n" +
            "WHERE 1 = 1 ${whereSql} ${sortSql} LIMIT ${nowPage}, ${pageSize}")
    List<OrderModel> findListBySqlJoin(Map<String, Object> params);

    @Select("SELECT count(1) FROM " +
            "\tbiz_order t \n" +
            "LEFT JOIN biz_user u ON t.user_id = u.id \n" +
            "LEFT JOIN biz_user iu ON t.invest_id = iu.id\n" +
            "LEFT JOIN biz_agent ag ON t.agent_id = ag.id\n" +
            "WHERE 1 = 1 ${whereSql} ")
    long countBySqlJoin(Map<String, Object> params);

    @Select("SELECT COUNT(1) FROM ( SELECT COUNT(1) AS c FROM biz_order o WHERE 1=1 ${whereSql} GROUP BY o.user_id) t WHERE 1=1")
    Long findUserSum(Map<String, Object> params);

    @Select("SELECT SUM(o.number) FROM biz_order o WHERE 1=1 ${whereSql}")
    Long findNumSum(Map<String, Object> params);

    @Select("SELECT SUM(o.profit) FROM biz_order o WHERE 1=1 ${whereSql}")
    BigDecimal findProfitSum(Map<String, Object> params);

    @Select("SELECT SUM(o.profit+o.deposit) FROM biz_order o WHERE  1=1 AND o.profit<0 AND o.profit< -o.deposit ${whereSql}")
    BigDecimal findPenetrateSum(Map<String, Object> params);

    @Select("SELECT SUM(o.number*o.fee) FROM biz_order o WHERE  1=1 ${whereSql}")
    BigDecimal findFeeSum(Map<String, Object> params);

    @Select("SELECT SUM((1-o.allot_rate)*o.profit) FROM biz_order o WHERE  1=1 AND o.profit>0 ${whereSql}")
    BigDecimal findDividedSum(Map<String, Object> params);

    /**
     * 查询同一订单拆分的所有订单号
     *
     * @param orderNo 原始订单号
     * @return
     */
    @Select("SELECT order_no from biz_order where order_no like concat(#{orderNo},'%') and del_flag = 1")
    List<String> selectOrderNos(@Param(value = "orderNo") String orderNo);

    /**
     * 前台跟单持仓列表查询
     *
     * @param params
     * @return
     */
    @Select("<script>SELECT COUNT(1) FROM biz_order t WHERE t.del_flag = #{delFlag} AND t.`status` in (#{status}<if test=\"status2 != null and status2 != ''\"> ,#{status2}</if>) AND t.user_id = #{userId} AND t.follow = #{follow}</script>")
    Integer findCountForOrderList(Map<String, Object> params);

    /**
     * 前台跟单持仓列表查询
     *
     * @param params
     * @return
     */
    @Select("<script>SELECT t.* FROM biz_order t WHERE t.del_flag = #{delFlag} AND t.`status` in (#{status}<if test=\"status2 != null and status2 != ''\"> ,#{status2}</if>) AND t.user_id = #{userId} AND t.follow = #{follow} ORDER BY t.id DESC </script>")
    List<OrderModel> findListForOrderList(Map<String, Object> params);

    /**
     * 查询达到止盈止损线的订单
     *
     * @param np   最新价
     * @param code 品种类型
     * @return
     */
    @Select("SELECT * FROM biz_order  WHERE del_flag = 1 AND `status` = 3 AND code = #{code} AND ((direction = 1 AND ( lose_stop_price >= #{np} OR profit_stop_price <= #{np})) OR (direction = 2 AND ( lose_stop_price <= #{np} OR profit_stop_price >= #{np})))")
    List<Order> selectByLostProfit(@Param(value = "np")BigDecimal np, @Param(value = "code")String code);

    /**
     * 查询订单数量（有效的）
     *
     * @param userId 用户ID
     * @return
     *
     */
    @Select("SELECT t.* FROM biz_order t WHERE t.`status` in (3,4,5,6) AND t.user_id = #{userId} AND t.del_flag = 1")
    List<Order> selectSucByUID(@Param(value = "userId") Integer userId);

    /**
     * 根据状态和用户id 查询
     */
    @Select("SELECT t.* FROM biz_order t WHERE t.`status` in (${statusSql}) AND t.user_id = #{userId} AND t.del_flag = 1")
    List<Order> selectByStatusUserId(Map<String, Object> params);

    /**
     * 查询待流单的订单
     */
    @Select("SELECT t.* FROM biz_order t WHERE t.`status` = #{status} AND TIMESTAMPDIFF(SECOND,t.create_time,NOW()) > 5  AND t.del_flag = 1")
    List<Order> selectWaitFlowOrder(Map<String, Object> params);

    /**
     * 查询跟单待撤单的订单
     */
    @Select("SELECT t.* FROM biz_order t WHERE t.order_no like #{orderNo} AND t.`status` = 2 AND t.del_flag = 1")
    List<Order> selectLikeOrderNoT2(@Param(value = "orderNo")String orderNo);

    /**
     * 查询跟单待平仓的订单
     */
    @Select("SELECT t.* FROM biz_order t WHERE t.order_no like #{orderNo} AND t.`status` = 3 AND t.del_flag = 1")
    List<Order> selectLikeOrderNoT3(@Param(value = "orderNo")String orderNo);

}
